﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineFloralShop.Product
{
    public partial class Details : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ProductId"] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    //Set title
                    Title = Model.Catalog.Product.GetProductDetailByProductId(Request.QueryString["ProductId"].ToString()).Rows[0]["ProductName"].ToString() + " - Online Floral Shop";
                    //Set datasource and binding
                    FormView_Details.DataSource = Model.Catalog.Product.GetProductDetailByProductId(Request.QueryString["ProductId"].ToString());
                    FormView_Details.DataBind();

                    //Set datasource and Binding Products Relate
                    ListView_ProductRelate.DataSource = Model.Catalog.Product.GetProductsRelate(Request.QueryString["ProductId"]);
                    ListView_ProductRelate.DataBind();

                    //Review
                    PagingReview();
                }
            }
           lbnotify.Text = "";
        }

        protected void btnAddtoCart_Click(object sender, EventArgs e)
        {
            if (((TextBox)FormView_Details.FindControl("txtQuantity")).Text != "" && int.Parse(((TextBox)FormView_Details.FindControl("txtQuantity")).Text)>0)
            {
                Response.Redirect("~/AddToCart.aspx?ProductId=" + Request.QueryString["ProductId"].ToString() + "&Quantity=" + ((TextBox)FormView_Details.FindControl("txtQuantity")).Text);
            }
        }

        protected void btnSendcmt_Click(object sender, EventArgs e)
        {
            Model.Catalog.Product.AddReview(Request.QueryString["ProductId"].ToString(), txtAuthor.Text, txtEmail.Text, txtContent.Text);
            lbnotify.Text = "* Your comment will be checked before show!Thank you";
        }

        //-------------------------------------------------------------//
        //Paging Review
        void PagingReview()
        {
            //total recorde
            int total = int.Parse(Model.Catalog.Product.GetCountReviewByProductId(Request.QueryString["ProductId"]).Rows[0]["CountReview"].ToString());
            int size = 3, page;

            //set page
            if (total % size > 0)
                page = total / size + 1;
            else
                page = total / size;


            //Set datasource and Binding Review
            if (Request.QueryString["Page"] != null)
            {
                ListView_Review.DataSource = Model.Catalog.Product.GetReviewByProductId(Request.QueryString["ProductId"].ToString(), int.Parse(Request.QueryString["Page"]), size);
                ListView_Review.DataBind();
            }
            else
            {
                ListView_Review.DataSource = Model.Catalog.Product.GetReviewByProductId(Request.QueryString["ProductId"].ToString(), 1, size);
                ListView_Review.DataBind();
            }

            
            
            int current = 1;
            //Previous link
            if (Request.QueryString["Page"] != null && int.Parse(Request.QueryString["Page"])!=1 && page>1)
                lbPrev.Text = "<a href='Details.aspx?ProductId=" + Request.QueryString["ProductId"] + "&Page=" + (int.Parse(Request.QueryString["Page"]) - 1) + "' > Prev </a>" + "   ";
     
            //Numeric link
            for (int i = 1; i <= page; i++)
            {
                if (Request.QueryString["Page"] != null)
                    current = int.Parse(Request.QueryString["Page"]);
                else
                    current = 1;
                if (i != current)
                    lbPaging.Text += "<a href='Details.aspx?ProductId=" + Request.QueryString["ProductId"] + "&Page=" + i + "' >" + i + "</a>" + "   ";
                else
                    lbPaging.Text += i + " ";
            }
            //Next link
            if (Request.QueryString["Page"] != null)
            {
                lbNext.Text = "<a href='Details.aspx?ProductId=" + Request.QueryString["ProductId"] + "&Page=" + (int.Parse(Request.QueryString["Page"]) + 1) + "' > Next </a>" + "   ";
                if (int.Parse(Request.QueryString["Page"]) == page)
                {
                    lbNext.Text = "";
                }
            }
            else
            {
                if (page > 1)
                {
                    lbNext.Text = "<a href='Details.aspx?ProductId=" + Request.QueryString["ProductId"] + "&Page=" + 2 + "' > Next </a>" + "   ";
                }
                else
                {
                    lbNext.Text = "";
                }
            }
         
            
        }
    }
}
